package me.dwliu.lab.demo.webflux.service.impl;

import lombok.AllArgsConstructor;
import me.dwliu.lab.demo.webflux.dao.EmployeeRepository;
import me.dwliu.lab.demo.webflux.model.Employee;
import me.dwliu.lab.demo.webflux.service.EmployeeService;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 业务层实现
 *
 * @author liudw
 * @date 2020-03-29 09:32
 **/
@Service
@AllArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
    private final EmployeeRepository employeeRepository;

    public Mono<Employee> create(Employee entity) {
//        employeeRepository.insert(entity);
        return employeeRepository.save(entity);
    }

    public Mono<Employee> findById(Long id) {
        return employeeRepository.findById(id);
    }

    public Flux<Employee> findByName(String name) {
        return employeeRepository.findByName(name);
    }

    public Flux<Employee> findAll() {
        return employeeRepository.findAll();
    }

    public Mono<Employee> update(Employee entity) {
        return employeeRepository.save(entity);
    }

    public Mono<Void> delete(Long id) {
        return employeeRepository.deleteById(id);

//        return cityRepository.deleteById(id).flatMap(mono -> Mono.create(cityMonoSink -> cityMonoSink.success(id)));
    }
}
